home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / share / 04 / setup.exe / MM9.Cab / F1486_ado.runtime5.asp.9B3B646D_CB56_4EAE_BAB7_3E7E8E41A649 < prev    next >
Text File  |  2000-08-17  |  13KB  |  452 lines

  1. <SCRIPT runat="server" language="VBScript">
  2.  
  3. ' *****************************************************************************
  4. '
  5. ' include/ado.runtime5.asp
  6. '
  7. ' Dynamic Link runtime support for Microsoft ADO.
  8. '
  9. ' COPYRIGHT (c) 1999-2000 Adobe Systems Incorporated. All rights reserved.
  10. '
  11. ' -----------------------------------------------------------------------------
  12.  
  13. function GetMaxPendingRows( rs )
  14.     GetMaxPendingRows = 0
  15.     if False = RuntimeDebug then
  16.         on error resume next
  17.     end if
  18.     GetMaxPendingRows = rs.Properties( "Maximum Pending Rows" ).Value
  19. end function
  20.  
  21. function testIndexColumns( index )
  22.     if False = RuntimeDebug then
  23.         on error resume next
  24.     end if
  25.     count = index.Columns.Count
  26.     if Err then
  27.         testIndexColumns = false
  28.     else
  29.         testIndexColumns = true
  30.     end if
  31. end function
  32.  
  33.  
  34. </SCRIPT>
  35.  
  36. <SCRIPT runat="server" language="JScript">
  37.  
  38. // *****************************************************************************
  39. // CONSTANTS
  40. //
  41.  
  42. // CursorTypeEnum
  43. var adOpenForwardOnly    = 0;
  44. var adOpenKeyset        = 1;
  45. var adOpenDynamic        = 2;
  46. var adOpenStatic        = 3;
  47. var adOpenUnspecified    = -1;
  48.  
  49. // LockTypeEnum
  50. var adLockReadOnly            = 1;
  51. var adLockPessimistic        = 2;
  52. var adLockOptimistic        = 3;
  53. var adLockBatchOptimistic    = 4;
  54. var adLockUnspecified        = -1;
  55.  
  56. // DataTypeEnum
  57. var AdArray                = 0x2000;
  58. var adEmpty                = 0;
  59. var adSmallInt            = 2;
  60. var adInterger            = 3;
  61. var adSingle            = 4;
  62. var adDouble            = 5;
  63. var adCurrency            = 6;
  64. var adDate                = 7;
  65. var adBSTR                = 8;
  66. var adIDispatch            = 9;
  67. var adError                = 10;
  68. var adBoolean            = 11;
  69. var adVariant            = 12;
  70. var adIUnknown            = 13;
  71. var adDecimal            = 14;
  72. var adTinyInt            = 16;
  73. var adUnsignedTinyInt    = 17;
  74. var adUnsignedSmallInt    = 18;
  75. var adUnsignedInt        = 19;
  76. var adBigInt            = 20;
  77. var adUnsignedBigInt    = 21;
  78. var adFileTime            = 64;
  79. var adGUID                = 72;
  80. var adBinary            = 128;
  81. var adChar                = 129;
  82. var adWChar                = 130;
  83. var adNumeric            = 131;
  84. var adUserDefined        = 132;
  85. var adDBDate            = 133;
  86. var adDBTime            = 134;
  87. var adDBTimeStamp        = 135;
  88. var adChapter            = 136;
  89. var adPropVariant        = 138;
  90. var adVarNumeric        = 139;
  91. var adVarChar            = 200;
  92. var adLongVarChar        = 201;
  93. var adVarWChar            = 202;
  94. var adLongVarWChar        = 203;
  95. var adVarBinary            = 204;
  96. var adLongVarBinary        = 205;
  97.  
  98.  
  99. var adSchemaTables = 20;
  100. var adSchemaProviderTypes = 22;
  101.  
  102. // *****************************************************************************
  103. // CONTENT SOURCE WRAPPER: RECORDSET
  104. //
  105. // Content source wrapper for RecordSet.  The primaryKey (a comma separated list
  106. // of column names) must be specified for record sets which will be updated; it 
  107. // is ignored for those that are not.
  108. //
  109. // Items are accessed through the wrapper as follows:
  110. //
  111. //      set results = WrapRecordSet(adoRecordSet, primaryKey)
  112. //      results.MoveFirst
  113. //      results.Value("SKU")
  114.  
  115. function WrapRecordSet( adoRecordSet, primaryKey )
  116. {
  117.     var wrapper = new CSWRecordSet( adoRecordSet, primaryKey );
  118.     var key = Request.QueryString( "RECORD_KEY" );
  119.     if( key.Count > 0 ){
  120.         wrapper.MoveToKey( key );
  121.     }
  122.     return wrapper;
  123. }
  124.  
  125.  
  126. //    First  priority: User specified primary key field
  127. //    Second priority: Automatic discovery primary key for OLE DB (ODBC is not supported)
  128. //    Third  priority: Use all field without long text
  129. function CSWRecordSet(adoRecordSet, primaryKey)
  130. {
  131.     // Method table:
  132.     this.Move         = CSWRecordSet_Move;
  133.     this.MoveFirst     = CSWRecordSet_MoveFirst;
  134.     this.MoveNext     = CSW_MoveNext;
  135.     this.Value         = CSWRecordSet_Value;
  136.     this.Set         = CSWRecordSet_Set;
  137.     this.Key         = CSWRecordSet_Key;
  138.     this.MoveToKey     = CSWRecordSet_MoveToKey;
  139.     this.UpdateBatch = CSWRecordSet_UpdateBatch;
  140.     this.AddNew         = CSWRecordSet_AddNew;
  141.     this.Delete         = CSWRecordSet_Delete;
  142.  
  143.     this.Data        = adoRecordSet;
  144.  
  145.     this.maxpending = GetMaxPendingRows( adoRecordSet );
  146.  
  147.     if( primaryKey ){    //    User specified primary key
  148.         var temp = String( primaryKey ).split( " " ).join( "" );
  149.         this.PrimaryKey = temp.split( "," );
  150.     }else{    //    Primary key is not specified >> auto detect
  151.         if( RuntimeDebug ){
  152.             RuntimeDebugMessage( "No primary key is specified" );
  153.         }
  154.         this.PrimaryKey = new Array;
  155.         var catalog = new ActiveXObject( "ADOX.Catalog" );
  156.         catalog.ActiveConnection = adoRecordSet.ActiveConnection;
  157.         var tableName = getTableNameFromSource( adoRecordSet.Source );
  158.         var table    = new ActiveXObject( "ADOX.Table" );
  159.         table = catalog.Tables.Item( String( tableName ));
  160.  
  161.         var    primaryKeyCounter = 0;
  162.         var    indexColumnsSupport = true;
  163.  
  164.         for( var k = 0; k < table.Indexes.Count; k++ ){
  165.             var index = new ActiveXObject( "ADOX.Index" );
  166.             index = table.Indexes.Item( k );
  167.             if( !index.PrimaryKey )    continue;    //    skip non-primary key
  168.             if( testIndexColumns( index )){    // supported
  169.                 for( var m = 0; m < index.Columns.Count; m++ ){
  170.                     var column    = new ActiveXObject( "ADOX.Column" );
  171.                     column = index.Columns.Item( m );
  172.                     var n = 0;
  173.                     for( n = 0; n < table.Columns.Count; n++ ){
  174.                         var column2     = new ActiveXObject( "ADOX.Column" );
  175.                         column2 = table.Columns.Item( n );
  176.                         if( column.Name == column2.Name )    break;
  177.                     }
  178.                     if( n < table.Columns.Count ){
  179.                         var o = 0;
  180.                         for( o = 0; o < this.PrimaryKey.length; o++ ){
  181.                             if( column.Name == this.PrimaryKey[o].Name ) break;
  182.                         }
  183.                         if( this.PrimaryKey.length <= o ){
  184.                             this.PrimaryKey[primaryKeyCounter++] = column.Name;
  185.                         }
  186.                     }
  187.                 }
  188.             }else{    //    not supported
  189.                 indexColumnsSupport = false;
  190.                 break;
  191.             }
  192.         }
  193.         if( !indexColumnsSupport ||( primaryKeyCounter == 0 )){        //    failed automatic discover primary key or no primary key
  194.             var knownkey = false;
  195.             for( var i = 0; i < adoRecordSet.fields.count; i++ ){
  196.                 var field = adoRecordSet.fields.item( i );
  197.                 var attrib = field.Attributes;
  198.                 if(( attrib & 0x100 )&&( attrib != -1 )){
  199.                     this.PrimaryKey = new Array( field.Name );
  200.                     break;
  201.                 }
  202.                 if( knownkey ){
  203.                     if(( attrib & 0x8000 )&&( attrib != -1 )){
  204.                         this.PrimaryKey = this.PrimaryKey.concat( Array( field.Name ));
  205.                     }
  206.                 }else{
  207.                     if( attrib == -1 ){
  208.                         this.PrimaryKey = this.PrimaryKey.concat( Array( field.Name ));
  209.                     }else if( attrib & 0x8000 ){
  210.                        this.PrimaryKey = new Array( field.Name );
  211.                         knownkey = true;
  212.                     }else if( !( attrib & 0x80 )){
  213.                         this.PrimaryKey = this.PrimaryKey.concat( Array( field.Name ));
  214.                     }
  215.                 }
  216.             }
  217.         }
  218.     }
  219.     this.RecordCount = 0;
  220.     while( !adoRecordSet.EOF ){
  221.         this.RecordCount++;
  222.         adoRecordSet.MoveNext();
  223.     }
  224.     this.NewRecord = false;
  225.     this.BlockSize = 0;
  226.     this.MoveFirst();
  227. }
  228.  
  229. // -----------------------------------------------------------------------------
  230. // Cursor positioning:
  231.  
  232. function CSWRecordSet_Move( delta )
  233. {
  234.     if( delta > 0 ){
  235.         this.AbsolutePosition += Number( delta );
  236.         this.RECORD_INDEX = this.AbsolutePosition;
  237.         this.EOF = ( this.AbsolutePosition > this.RecordCount );
  238.         this.Data.Move( delta );
  239.     }
  240. }
  241.  
  242. function CSWRecordSet_MoveFirst()
  243. {
  244.     this.AbsolutePosition = Number( 1 );
  245.     this.RECORD_INDEX = this.AbsolutePosition;
  246.     this.EOF = ( this.AbsolutePosition > this.RecordCount );
  247.     if( this.RecordCount > 0 ){
  248.         this.Data.MoveFirst();
  249.     }
  250. }
  251.  
  252. // -----------------------------------------------------------------------------
  253. // Data access:
  254.  
  255. function CSWRecordSet_Value(fieldName)
  256. {
  257.     if( this.EOF ){
  258.         return "";    // new record
  259.     }
  260.     return this.Data.fields.item( fieldName );
  261. }
  262.  
  263. function CSWRecordSet_Set(fieldName, value)
  264. {
  265.     if( String( value ).length == 0 ){
  266.         value = null;
  267.     }
  268.     for( var i = 0; i < this.Data.fields.count; i++ ){
  269.         if( this.Data.fields.item( i ).name.toLowerCase() == fieldName.toLowerCase() ){
  270.             if(( this.AbsolutePosition != this.LastEdit )&& this.maxpending ){
  271.                 this.Data.UpdateBatch();
  272.             }
  273.             this.LastEdit = this.AbsolutePosition;
  274.             this.Data.fields.item( i ).value = value;
  275.             break;
  276.         }
  277.     }
  278. }
  279.  
  280. // -----------------------------------------------------------------------------
  281. // Key-based data access:
  282. //
  283. // The primary key specification is stored as an array of column names.     An
  284. // instance of a key is of the format: colName=value&colName=value....
  285.  
  286. function CSWRecordSet_Key()
  287. {
  288.     if( this.EOF ){
  289.         return "$newRecord";
  290.     }
  291.  
  292.     var keyLengthLimit = 255 - String( Request.ServerVariables( "PATH_TRANSLATED" )).length - 50;  // 50 is extra mergin included server name
  293.     var fieldLengthLimit = Math.max( 4, (( keyLengthLimit / this.PrimaryKey.length ) / 6 )); //     Min 4 is for "null". 6 is worst case for 2 byte char encoded.
  294.  
  295.     var key = "";
  296.     var keyField = "";
  297.     for( var i = 0; i < this.PrimaryKey.length; i++ ){
  298.         var keyName = this.PrimaryKey[i];
  299.         if( keyLengthLimit < ( key.length - keyName.length - fieldLengthLimit - 1 ))    break;
  300.         if( 0 < i ){
  301.             key += "&";
  302.             keyField += ",";
  303.         }
  304.         switch( this.Data.fields.item( keyName ).Type ){
  305.             case adDate:
  306.             case adDBDate:
  307.             case adDBTime:
  308.             case adDBTimeStamp:
  309.                 if(( this.Data.fields.item( keyName ) == null )||( String( this.Data.fields.item( keyName )).toUpperCase() == "NULL" )){
  310.                     key += escape( keyName ) + "=" + escape( "null" );
  311.                     keyField += keyName;
  312.                 }else{
  313.                     key += escape( keyName ) + "=" + escape( callVBFormatDateTime( this.Data.fields.item( keyName )));
  314.                     keyField += keyName;
  315.                 }
  316.                 break;
  317.             case adBSTR:
  318.             case adChar:
  319.             case adWChar:
  320.             case adVarChar:
  321.             case adLongVarChar:
  322.             case adVarWChar:
  323.             case adLongVarWChar:
  324.                 if(( this.Data.fields.item( keyName ) == null )||( String( this.Data.fields.item( keyName )).toUpperCase() == "NULL" )){
  325.                     key += escape( keyName ) + "=" + escape( "null" );
  326.                     keyField += keyName;
  327.                 }else{
  328.                     key += escape( keyName ) + "=" + escape( String( this.Data.fields.item( keyName )).substr( 0, fieldLengthLimit ));
  329.                     keyField += keyName;
  330.                 }
  331.                 break;
  332.             default:
  333.                 if(( this.Data.fields.item( keyName ) == null )||( String( this.Data.fields.item( keyName )).toUpperCase() == "NULL" )){
  334.                     key += escape( keyName ) + "=" + escape( "null" );
  335.                     keyField += keyName;
  336.                 }else{
  337.                     key += escape( keyName ) + "=" + escape( this.Data.fields.item( keyName ));
  338.                     keyField += keyName;
  339.                 }
  340.                 break;
  341.         }
  342.     }
  343.     if( RuntimeDebug ){
  344.         var testString = escape( keyField ) + "&" + key;
  345.         if( keyLengthLimit < testString.length ){
  346.             RuntimeDebugMessage( "Generated key length is too long." );
  347.         }
  348.     }
  349.     return escape( keyField ) + "&" + key;
  350. }
  351.  
  352.  
  353. function CSWRecordSet_MoveToKey( key )
  354. {
  355.     if( key == "$newRecord" ){
  356.         while( !this.EOF ){
  357.             this.Move( 1 );
  358.         }
  359.     }else{
  360.         this.MoveFirst();
  361.         var keys = new Array;
  362.         if( key == "$queryString" ){    //    this part is version 1 compatibility.
  363.             var keyString = Request.QueryString( "$key" );
  364.             if( keyString.Count < 1 )    return;
  365.             var rawKeys = String( unescape( keyString )).split( "&" );
  366.             for( var i = 0; i < rawKeys.length; i++ ){
  367.                 var keyDelim = rawKeys[i].indexOf( "=" );
  368.                 keys[i]          = new Object;
  369.                 keys[i].name  = unescape( rawKeys[i].substring( 0, keyDelim ));
  370.                 keys[i].value = unescape( rawKeys[i].substring( keyDelim + 1 ));
  371.             }
  372.         }else{
  373.             var keyName = String( key ).split( "," );
  374.             for( var i = 0; i < keyName.length; i++ ){
  375.                 keys[i]          = new Object;
  376.                 keys[i].name  = keyName[i];
  377.                 keys[i].value = String( Request.QueryString( keyName[i] ));
  378.             }
  379.         }
  380.         while( !this.EOF ){
  381.             var match = true;
  382.             for( var i = 0; i < this.Data.fields.count; i++ ){
  383.                 var field = this.Data.fields.item( i );
  384.                 var stringField = String( field );
  385.                 if(( field.Type == adDate )||( field.Type == adDBDate )||( field.Type == adDBTime )||( field.Type == adDBTimeStamp )) stringField = callVBFormatDateTime( field );
  386.                 for( var j = 0; match && j < keys.length; j++ ){
  387.                     if(( field.Name == keys[j].name )&&( stringField.indexOf( keys[j].value ) != 0 )){
  388.                         match = false;
  389.                     }
  390.                 }
  391.             }
  392.             if( match ){
  393.                 return;
  394.             }
  395.             this.MoveNext();
  396.         }
  397.     }
  398. }
  399.  
  400. // -----------------------------------------------------------------------------
  401. // Forms automation:
  402.  
  403. function obscure( plaintext )
  404. {
  405.     var plain = "obscure_" + escape( plaintext );
  406.     if( typeof( Application( plain )) == "undefined" )
  407.     {
  408.         Application( plain ) = Math.random();
  409.         Application( "obscure_" + Application( plain )) = plaintext;
  410.     }
  411.     return Application( plain );
  412. }
  413.  
  414. function unobscure( hash )
  415. {
  416.     return Application( "obscure_" + hash );
  417. }
  418.  
  419.  
  420. function CSWRecordSet_UpdateBatch()
  421. {
  422.     this.Data.UpdateBatch();
  423. }
  424.  
  425. function CSWRecordSet_Delete()
  426. {
  427.     this.Data.Delete();
  428. }
  429.  
  430. function CSWRecordSet_AddNew()
  431. {
  432.     this.Data.AddNew();
  433. }
  434.  
  435.  
  436. function getTableNameFromSource( source )
  437. {
  438.     var tableName = "";
  439.     var tokens = source.split( " " );
  440.     for (var i = 0; i < ( tokens.length -1 ); i++) {
  441.         if( tokens[i].toUpperCase() == "FROM" ){
  442.             tableName = tokens[i+1];
  443.             break;
  444.         }
  445.     }
  446.     return tableName;
  447. }
  448.  
  449.  
  450. </SCRIPT>
  451.  
  452.